//
//  SelectDistributionViewController.m
//  cityStar
//
//  Created by joyban on 14-12-30.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "SelectDistributionViewController.h"

#import "SelectDistributionOneCell.h"
#import "SelectDistributionTwoCell.h"

#import "UIColor+FlatUI.h"

NSString *const selectDistributionOneID = @"SelectDistributionOneID";
NSString *const selectDistributionTwoID = @"SelectDistributionTwoID";

@interface SelectDistributionViewController ()
@property (weak, nonatomic) IBOutlet UITableView *SelectDistribution;
@property (strong, nonatomic) NSMutableArray *cellArray;

@property (strong, nonatomic) SelectDistributionOneCell *selectDistributionOneCell;

@end

@implementation SelectDistributionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择配送方式";
    [self addBackButton];
    [self setUpTableViews];
    self.cellArray = [[NSMutableArray alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.SelectDistribution registerNib:[SelectDistributionOneCell nib] forCellReuseIdentifier:selectDistributionOneID];
    [self.SelectDistribution registerNib:[SelectDistributionTwoCell nib] forCellReuseIdentifier:selectDistributionTwoID];
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return [SelectDistributionOneCell heightOfTableCell];
    }else{
        return [SelectDistributionTwoCell heightOfTableCell];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }else{
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)];
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FAF2E2"]];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, tableView.bounds.size.width-20, 30)];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:14];
    [sectionView addSubview:label];
    
    if(section == 0)
    {
        label.text = @"送货上门";
    }else{
        label.text = @"到自提点提货，请就近选择提货点";
    }
    
    return sectionView;
}


#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"saveSection"];
    if(indexPath.section == 0)
    {
         self.selectDistributionOneCell = [tableView dequeueReusableCellWithIdentifier:selectDistributionOneID];
        self.selectDistributionOneCell.selectionStyle = UITableViewCellSelectionStyleGray;
        
        if([str isEqualToString:@"0"])
        {
            self.selectDistributionOneCell.SelectImage.hidden = NO;
        }else{
            self.selectDistributionOneCell.SelectImage.hidden = YES;
        }
        
        return self.selectDistributionOneCell;
        
    }else{
        SelectDistributionTwoCell *selectDistributionTwoCell = [tableView dequeueReusableCellWithIdentifier:selectDistributionTwoID];
        selectDistributionTwoCell.selectionStyle = UITableViewCellSelectionStyleGray;
        if([str isEqualToString:@"1"])
        {
            selectDistributionTwoCell.selectImage.hidden = NO;
        }else{
            selectDistributionTwoCell.selectImage.hidden = YES;
        }
        
        if(indexPath.row < 1)
        {
            [selectDistributionTwoCell.lineImage setFrame:CGRectMake(20, 60, self.view.frame.size.width-20, 1)];
        }
        [self.cellArray addObject:selectDistributionTwoCell];
        return selectDistributionTwoCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"saveSection"];
    [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%li",indexPath.section] forKey:@"saveSection"];
    if(indexPath.section == 0)
    {
        self.selectDistributionOneCell.SelectImage.hidden = NO;
        for(SelectDistributionTwoCell *selectDistributionTwoCell in self.cellArray)
        {
            selectDistributionTwoCell.selectImage.hidden = YES;
        }
    }else{
        self.selectDistributionOneCell.SelectImage.hidden = YES;
        for(SelectDistributionTwoCell *selectDistributionTwoCell in self.cellArray)
        {
            selectDistributionTwoCell.selectImage.hidden = YES;
        }
        SelectDistributionTwoCell *selectTwoCell = (SelectDistributionTwoCell *)[tableView cellForRowAtIndexPath:indexPath];
        selectTwoCell.selectImage.hidden = NO;
    }
    if ([self.delegate respondsToSelector:@selector(selectDistributionVCChoosePeisongType:)]) {
        [self.delegate selectDistributionVCChoosePeisongType:[NSString stringWithFormat:@"%li",indexPath.section+1]];
    }
    [tableView reloadData];
    [self.navigationController popViewControllerAnimated:YES];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
